<?php

include( "./include/func.php");
include( "Pager/Pager.php");

ini_set( 'display_errors' , '1' );
ini_set( 'log_errors' , '0' );

$db = new db();
if ( ! $db->connect() ) exit( mysql_error() );

//enable/disable partner
if( isset( $_GET['ed'] ) ) {
	
	$id = (int) $_GET['ed'];
	
	$db->sql = "UPDATE linkcsere SET lcs_enabled=NOT(lcs_enabled) WHERE lcs_id='$id' LIMIT 1";	 
	$db->query();
	//d( $db->sql );	
	//d( mysql_error() );
}

$db->sql = "SELECT COUNT(lcs_id)
			FROM linkcsere
			WHERE lcs_enabled=1";	
$db->query(); 
//d( $db->sql );

$rpp = 100; //row per page

$tct = mysql_fetch_row( $db->rs );
$tct = $tct[0];	//total count

$pager_options = array( 'mode' => 'Sliding', 'perPage' => $rpp, 'delta' => 2, 'totalItems' => $tct );
$pager = @Pager::factory( $pager_options );
list( $from, $to ) = $pager->getOffsetByPageId();

$db->sql = "SELECT l.*,
			(SELECT SUM(r.ref_num) FROM referers r WHERE r.`ref_link`=l.lcs_linkhely) as ref_num
			FROM linkcsere l
			WHERE l.lcs_enabled=1
			ORDER BY l.lcs_vip DESC, ref_num DESC, l.lcs_regDate
			LIMIT " . ( $from - 1 ) . ", $rpp";				
$db->query();
//d( $db->sql );
//d( mysql_error() );

$rows = array();
while( $row = mysql_fetch_array( $db->rs ) ) {
	$rows[] = $row;
}
//d( $rows );

?>

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p>
	<a href="<?=$_SERVER["SCRIPT_NAME"]?>"><?=$tct?></a>
</p>

<table cellpadding="10" border="1">
	<tr>
	    <th>id</th>
	    <th>name</th>
        <th>link</th>
        <th>ref count</th>
	    <th>enabled</th>
        <th>Properties</th>
        <th>Operations</th>
	</tr>
	<? foreach($rows as $row): ?> 
    <tr>    	
        <td><?=$row['lcs_id']?></td>
        <td><?=$row['lcs_name']?></td>
        <td><a href="<?=$row['lcs_link']?>" target="_blank"><?=$row['lcs_caption']?></a> (<a href="<?=$row['lcs_linkhely']?>" target="_blank"><?=$row['lcs_linkhely']?></a>)</td>
        <td><?=$row['ref_num']?></td>
        <td><?=$row['lcs_enabled']?></td>
        <td>
        	<p>Pagerank:<img id="pr<?=$row['lcs_id']?>" src="./images/loader16.gif" /></p>
			<script>
			$.getJSON("get-pagerank.php?url=<?=str_replace('.','*',$row['lcs_link'])?>",
			  function(data) {
				$("#pr" + <?=$row['lcs_id']?>).replaceWith("<span>" + data.value + "</span>");
			  });
			</script>
        	<p>Found:</p>
        	<p>VIP:<?=$row['lcs_vip']?></p>
        	<p>RegDate:<?=$row['lcs_regDate']?></p>
        </td>
        <td>
        	<a href="<?=$_SERVER["SCRIPT_NAME"]?>?ed=<?=$row['lcs_id']?>">E/Disable</a>
        </td>
    </tr>
	<? endforeach;?>
</table>

</body>
</html>

<?php

function d ($var, $cap="") {
	print "$cap:<br /><pre>";
	print_r( $var );
	print "</pre><br /><br />";
}

?>
